import json
import requests

class cTelegram(): #29052024
    def __init__(self,GL1):
        """
        :See: ***
        """
        self.GL1=GL1
        #self.STR=f"https://api.telegram.org/bot{GL1['TOKEN']}"
        return
    def GetUpdates(self,AR1=None):
        """
        :See: https://core.telegram.org/bots/api#getupdates
        """
        self.CallbackQuery=None
        self.ChannelPost=None
        self.EditedChannelPost=None
        self.Message=None
        RE1={}
        if AR1 is not None and type(AR1)==int:
            RE1['offset']=AR1+1
        ##RE1['allowed_updates']='MessageReactionUpdated' https://core.telegram.org/bots/api#messagereactionupdated
        RE2=f"https://api.telegram.org/bot{self.GL1['TOKEN']}/getUpdates"
        try:
            RE3=requests.post(RE2,data=RE1)
        except Exception as Error:
            print(f"28 {Error}")
            return
        RE4=RE3.json()
        #print(f"28 {RE4}")
        if 'ok' in RE4:
            if RE4['ok']==True:
                if 'result' in RE4:
                    if len(RE4['result'])>0:
                        if 'callback_query' in RE4['result'][0]:
                            print(f"37 {RE4['result'][0]['callback_query']['data']}")
                            self.CallbackQuery=RE4['result'][0]['callback_query']
                        elif 'channel_post' in RE4['result'][0]:
                            print(f"40 {RE4['result'][0]['channel_post']}")
                            self.ChannelPost=RE4['result'][0]['channel_post']
                        elif 'edited_channel_post' in RE4['result'][0]:
                            self.EditedChannelPost=RE4['result'][0]['edited_channel_post']
                        elif 'message' in RE4['result'][0]:
                            self.Message=RE4['result'][0]['message']
                        else:
                            print(f"47 {RE4}")
        if 'result' not in RE4:
            return AR1
        if len(RE4['result'])==0:
            return AR1
        if 'update_id' not in RE4['result'][0]:
            return AR1
        return RE4['result'][0]['update_id']
    def AnswerCallbackQuery(self,AR1=None,AR2=None): #24042024
        """
        :See: https://core.telegram.org/bots/api#answercallbackquery
        """
        RE1=f"https://api.telegram.org/bot{self.GL1['TOKEN']}/answerCallbackQuery"
        RE2={}
        RE2['callback_query_id']=AR1
        RE2['text']=AR2
        return self.RequestForTelegram(RE1,RE2)
    def CopyMessage(self,cid,fci,mid,prs='HTML',dnt=True,pcn=True): #24042024
        """
        :cid: chat_id
        :fci: from_chat_id
        :mid: message_id
        :prs: parse_mode='HTML'
        :dnt: disable_notification=True
        :pcn: protect_content=True
        :See: https://core.telegram.org/bots/api#copymessage
        """
        self.cid=cid
        self.fci=fci
        self.mid=mid
        self.prs=prs
        self.dnt=dnt
        self.pcn=pcn
        RE1=f"https://api.telegram.org/bot{self.GL1['TOKEN']}/copyMessage"
        RE2={}
        RE2['chat_id']=self.cid
        RE2['from_chat_id']=self.fci
        RE2['message_id']=self.mid
        RE2['parse_mode']=self.prs
        RE2['disable_notification']=self.dnt
        RE2['protect_content']=self.pcn
        return self.RequestForTelegram(RE1,RE2)
    def DeleteMessage(self,cid,mid):
        """
        :cid: chat_id
        :mid: message_id
        :See: https://core.telegram.org/bots/api#deletemessage
        """
        self.cid=cid
        self.mid=mid
        RE1=f"https://api.telegram.org/bot{self.GL1['TOKEN']}/deleteMessage"
        RE2={}
        RE2['chat_id']=self.cid
        RE2['message_id']=self.mid
        return self.RequestForTelegram(RE1,RE2)
    def SendMessage(self,cid,txt='',prs='HTML',rpl={},webpreview=True,dnt=True,pcn=False):
        """
        :cid: chat_id
        :txt: text
        :prs: parse_mode='HTML'
        :rpl: reply_markup
        :webpreview: disable_web_page_preview=True
        :dnt: disable_notification=True
        :pcn: protect_content=False
        :See: https://core.telegram.org/bots/api#sendmessage
        """
        self.cid=cid
        self.txt=txt
        self.prs=prs
        self.rpl=json.dumps(rpl)
        self.webpreview=webpreview #REWORK
        self.dnt=dnt
        self.pcn=pcn
        RE1=f"https://api.telegram.org/bot{self.GL1['TOKEN']}/sendMessage"
        RE2={}
        RE2['chat_id']=self.cid
        RE2['text']=self.txt
        RE2['parse_mode']=self.prs
        RE2['reply_markup']=self.rpl
        RE2['disable_web_page_preview']=self.webpreview
        RE2['disable_notification']=self.dnt
        RE2['protect_content']=self.pcn
        return self.RequestForTelegram(RE1,RE2)
    def EditMessageCaption(self,cid,mid,txt,prs='HTML',rpl={}):
        """
        :cid: chat_id
        :mid: message_id
        :txt: caption
        :prs: parse_mode='HTML'
        :rpl: reply_markup
        :See: https://core.telegram.org/bots/api#editmessagecaption
        """       
        self.cid=cid
        self.mid=mid
        self.txt=txt
        self.prs=prs
        self.rpl=json.dumps(rpl)
        RE1=f"https://api.telegram.org/bot{self.GL1['TOKEN']}/editMessageCaption"
        RE2={}
        RE2['chat_id']=self.cid
        RE2['message_id']=self.mid
        RE2['caption']=self.txt
        RE2['parse_mode']=self.prs
        RE2['reply_markup']=self.rpl
        return self.RequestForTelegram(RE1,RE2)
    def EditMessageText(self,cid,mid,txt,prs='HTML',rpl={}):
        #print(f"153 {txt}")
        """
        :cid: chat_id
        :mid: message_id
        :txt: text
        :prs: parse_mode='HTML'
        :rpl: reply_markup
        :See: https://core.telegram.org/bots/api#editmessagetext
        """
        self.cid=cid
        self.mid=mid
        self.txt=txt
        self.prs=prs
        self.rpl=json.dumps(rpl)
        RE1=f"https://api.telegram.org/bot{self.GL1['TOKEN']}/editMessageText"
        RE2={}
        RE2['chat_id']=self.cid
        RE2['message_id']=self.mid
        RE2['text']=self.txt
        RE2['parse_mode']=self.prs
        RE2['reply_markup']=self.rpl
        return self.RequestForTelegram(RE1,RE2)
    def RequestForTelegram(self,AR1,AR2):
        try:
            RE1=requests.post(AR1,data=AR2)
        except Exception as Error:
            print(f"179 {Error}")
            return
        RE2=RE1.json()
        return RE2
